﻿using System;
using System.Collections.Generic;
using Unread.App.ViewModels;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Unread.App.Views
{
    public sealed partial class AddFeedPage : Unread.App.Common.LayoutAwarePage
    {
        public AddFeedViewModel ViewModel
        {
            get
            {
                return DataContext as AddFeedViewModel;
            }
        }

        public AddFeedPage()
        {
            this.InitializeComponent();

            DataContext = ServicesFactory.GetInstance<AddFeedViewModel>();
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (pageState == null)
                return;

            ViewModel.Url = pageState["Url"] as string;
        }

        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            pageState["Url"] = ViewModel.Url;
        }

        private void Grid_Loaded_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            Url.Focus(FocusState.Programmatic);
        }

        private void BottomAppBar1_Loaded(object sender, RoutedEventArgs e)
        {
            RegisterAppBarButtons();
        }

        private void RegisterAppBarButtons()
        {
            Grid foo = BottomAppBar.Content as Grid;
            foreach (UIElement e in foo.Children)
            {
                foreach (UIElement c in ((StackPanel)e).Children)
                {
                    if (c.GetType() == typeof(Button))
                    {
                        base.StartLayoutUpdates(c, new RoutedEventArgs());
                    }
                }
            }
        }
    }
}
